define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/shareholder/index',
                    add_url: 'merchant/shareholder/add',
                    edit_url: 'merchant/shareholder/edit',
                    del_url: 'merchant/shareholder/del',
                    multi_url: 'merchant/shareholder/multi',
                    table: 'shareholder',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'uid', title: '股东ID'},
                        {field: 'puid', title: '上级股东ID'},
                        {field: 'lv', title: '股东等级'},
                        {field: 'createtime', title: '创建时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'updatetime', title: '更新时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            var examine = $('#examineSelect').val();
            var backUrlPanel = $('#backUrlPanel');
            var subMchidPanel = $('#subMchidPanel');
            if(examine == window.facescan) {
                backUrlPanel.show();
            } else {
                backUrlPanel.remove().show();
            }
            if(examine == window.complete) {
                subMchidPanel.show();
            } else {
                subMchidPanel.remove().show();
            }
            $('#edit-form').on('change', '#examineSelect', function() {
                if($(this).val() == window.facescan) {
                    backUrlPanel.insertBefore('#buttonPanel');
                } else {
                    backUrlPanel.remove().find('input').val('');
                }
                if($(this).val() == window.complete) {
                    subMchidPanel.insertBefore('#buttonPanel');
                } else {
                    subMchidPanel.remove().find('input').val('');
                }
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});